package com.ramos.kuaixiao.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ramos.kuaixiao.App;
import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.db.WareHouseDB;
import com.ramos.kuaixiao.entity.OrderDetail;
import com.ramos.kuaixiao.utils.FileUtil;
import com.ramos.kuaixiao.utils.StringUtil;
import com.ramos.kuaixiao.utils.Utils;

import java.util.List;

public class OrderItemAdapter extends BaseAdapter {

    private List<OrderDetail> tasklist;
    private Context context;
    private String mStoreHouseName = "";
    private boolean mIsDMS;
    static class ViewHolder {
        TextView productName;
        TextView productId;
        TextView productBarCode;
        TextView storeHouseName;
        TextView smallUnitCount;
        TextView unitSalePrice;
        TextView productSalePrice;
        TextView comment;
        ImageView im_product;
    }

    public OrderItemAdapter(Context context, List<OrderDetail> hotcourse, String storeHouseId, boolean isDMS) {
        this.context = context;
        this.tasklist = hotcourse;
        mIsDMS = isDMS;
        if(!StringUtil.isEmpty(storeHouseId)) {
            mStoreHouseName = new WareHouseDB().findWareHouse(storeHouseId);
        }
    }

    public void refresh(List<OrderDetail> data) {
        tasklist = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return tasklist.size();
    }

    @Override
    public Object getItem(int position) {
        return tasklist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.order_item, null);
            holder = new ViewHolder();
            holder.productName = (TextView) convertView.findViewById(R.id.productName);
            holder.im_product = (ImageView) convertView.findViewById(R.id.im_product);
            holder.productId = (TextView) convertView.findViewById(R.id.productId);
            holder.productBarCode = (TextView) convertView.findViewById(R.id.productBarCode);
            holder.storeHouseName = (TextView) convertView.findViewById(R.id.storeHouseName);

            holder.smallUnitCount = (TextView) convertView.findViewById(R.id.smallUnitCount);
            holder.unitSalePrice = (TextView) convertView.findViewById(R.id.unitSalePrice);
            //holder.productSalePrice = (TextView) convertView.findViewById(R.id.productSalePrice);
            holder.comment = (TextView) convertView.findViewById(R.id.comment);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.productName.setText(tasklist.get(position).getProductInfoName());
        Drawable icon = context.getResources().getDrawable(R.drawable.arp);
        //setBounds(left,top,right,bottom)里的参数从左到右分别是
        //drawable的左边到textview左边缘+padding的距离，drawable的上边离textview上边缘+padding的距离
        //drawable的右边边离textview左边缘+padding的距离，drawable的下边离textview上边缘+padding的距离
        //所以right-left = drawable的宽，top - bottom = drawable的高
        icon.setBounds(10, 0, 60, 50);
        Log.d("order", "productId:" + tasklist.get(position).getProductInfoId());
        holder.productName.setCompoundDrawables(icon, null, null, null);
        holder.productId.setText("编码:" + tasklist.get(position).getProductInfoId());
        holder.productBarCode.setText("条码:" + tasklist.get(position).getProductBarCode());
        String imgUrl = Utils.findFiles(FileUtil.getImagePath(App.mContext) + "/downloadimg", tasklist.get(position).getProductInfoId(), tasklist.get(position).getProductBarCode());

        Utils.glideImage(imgUrl, holder.im_product);
        if(StringUtil.isEmpty(tasklist.get(position).getStoreHouseName())){
            holder.storeHouseName.setText("仓库:" + mStoreHouseName);
        } else {
            holder.storeHouseName.setText("仓库:" + tasklist.get(position).getStoreHouseName());
        }
        int total = Integer.valueOf(tasklist.get(position).getLargeUnitCount()) * Integer.valueOf(tasklist.get(position).getCalcParam())
                + Integer.valueOf(tasklist.get(position).getSmallUnitCount());
        if(mIsDMS){
            int box = Integer.valueOf(tasklist.get(position).getSmallUnitCount()) / Integer.valueOf(tasklist.get(position).getCalcParam());
            int single = Integer.valueOf(tasklist.get(position).getSmallUnitCount())% Integer.valueOf(tasklist.get(position).getCalcParam());
            holder.smallUnitCount.setText("数量:" + box+ "箱" +
                    single + "支" + "(共:" + tasklist.get(position).getSmallUnitCount() + "支)");
        }else {
            holder.smallUnitCount.setText("数量:" + tasklist.get(position).getLargeUnitCount() + "箱" +
                    tasklist.get(position).getSmallUnitCount() + "支" + "(共:" + total + "支)");
        }

        if(StringUtil.isEmpty(tasklist.get(position).getReasons())) {
            holder.unitSalePrice.setText("￥" + App.df.format(Double.valueOf(tasklist.get(position).getUnitSalePrice())) + "(共:￥" + App.df.format(Double.valueOf(tasklist.get(position).getProductSalePrice())) + ")");
        } else {
            holder.unitSalePrice.setText("￥" + App.df.format(Double.valueOf(tasklist.get(position).getUnitSalePrice())) + "(共:￥" + App.df.format(Double.valueOf(tasklist.get(position).getProductSalePrice())) + ")" + " (库存不足,数量有变)");
        }
        //holder.unitSalePrice.setText("￥" + tasklist.get(position).getUnitSalePrice() + "(共:￥" + App.df.format(Double.valueOf(tasklist.get(position).getProductSalePrice())) + ")");
        //holder.productSalePrice.setText("总价:"+tasklist.get(position).getProductSalePrice());
        /*if(StringUtil.isEmpty(tasklist.get(position).getComment())){
            holder.comment.setText("备注:无");
        }else {
            holder.comment.setText("备注:" + tasklist.get(position).getComment() + "");
        }*/
        return convertView;
    }
}